package com.fita.admin.service.wallet.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.fita.admin.dao.AgentDao;
import com.fita.admin.dao.UserWalletDao;
import com.fita.admin.service.wallet.UserWalletService;
import com.fita.entity.agent.Agent;
import com.fita.entity.wallet.UserWallet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userWalletService")
public class UserWalletServiceImpl implements UserWalletService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Dao
    private AgentDao agentDao;

    @Dao
    private UserWalletDao userWalletDao;
    /**
     *
     * @param mobile
     * @return
     */
    public UserWallet loadByAgentMobile(String mobile) {

        Agent agent = agentDao.loadAgent(mobile);

        if (agent == null){
            throw BusinessException.newInstance("500", "该mobile未注册代理");
        }
        UserWallet userWallet = genericDaoSupport.searchForObject("FROM UserWallet WHERE agent = :agent", "agent", agent, UserWallet.class);

        return userWallet;
    }

    /**
     * 根据代理公司查找钱包
     *
     * @param agentCompanyId
     * @return
     */
    public UserWallet loadByAgentCompanyId(Long agentCompanyId) {
        return userWalletDao.loadByAgentCompanyId(agentCompanyId);
    }
}
